package com.gdth.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.List;

import com.gdth.model.Item;
import com.gdth.model.TreeItem;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

public class TreeViewAdapter extends BaseExpandableListAdapter{
	
    public static final int ItemHeight = 70;//每项的高度  
    
    public static final int PaddingLeft = 100;//每项的高度  
    
    private static AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ItemHeight);  

    private int myPaddingLeft = 10;//如果是由SuperTreeView调用，则作为子项需要往右移  
    
    OnChildClickListener childClickEvent;
    
    @SuppressWarnings("unused")
	private String key;
  
    static public class TreeNode{
        public Object parent;  
        public List<Object> childs=new ArrayList<Object>();
    }
      
    List<TreeNode> treeNodes = new ArrayList<TreeNode>();  
    Context parentContext;
    
    public TreeViewAdapter(Context view,int myPaddingLeft)  
    {  
        parentContext=view;
        this.myPaddingLeft=myPaddingLeft;
    }
    
    public TreeViewAdapter(Context view,int myPaddingLeft, OnChildClickListener childClickEvent, String key)  
    {  
        parentContext=view;
        this.myPaddingLeft=myPaddingLeft;
        
        this.childClickEvent = childClickEvent;
        this.key = key;
    }  
      
    public List<TreeNode> GetTreeNode()  
    {  
        return treeNodes;
    }  
      
    public void UpdateTreeNode(List<TreeNode> nodes)  
    {  
        treeNodes=nodes;
    }  
      
    public void RemoveAll()  
    {  
        treeNodes.clear();
    }  
      
    public Object getChild(int groupPosition, int childPosition) {  
        return treeNodes.get(groupPosition).childs.get(childPosition);  
    }  
  
    public int getChildrenCount(int groupPosition) {
    	if (treeNodes.get(groupPosition).childs == null) return 0;
        return treeNodes.get(groupPosition).childs.size();  
    }  
  
    static public TextView getTextView(Context context) {  
        TextView textView = new TextView(context);  
        textView.setLayoutParams(lp);  
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
        
        return textView;
    }  
  
    public View getChildView(int groupPosition, int childPosition,  
            boolean isLastChild, View convertView, ViewGroup parent) {  
        TextView textView = getTextView(this.parentContext); 
        Item mItem = (Item)getChild(groupPosition, childPosition);
        textView.setText(mItem.getValue());  
        textView.setTag(mItem.getKey()); 
        textView.setPadding(myPaddingLeft+PaddingLeft, 0, 0, 0);  
        return textView;  
    }  
  
    public View getGroupView(int groupPosition, boolean isExpanded,  
            View convertView, ViewGroup parent) { 
        TextView textView = getTextView(this.parentContext);  
        TreeItem nodes = (TreeItem) getGroup(groupPosition);
        textView.setText(nodes.val);  
        textView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);  
        return textView;  
    }  
  
    public long getChildId(int groupPosition, int childPosition) {  
        return childPosition;  
    }  
  
    public Object getGroup(int groupPosition) {  
        return treeNodes.get(groupPosition).parent;  
    }  
  
    public int getGroupCount() {  
        return treeNodes.size();  
    }  
  
    public long getGroupId(int groupPosition) {  
        return groupPosition;  
    }  
  
    public boolean isChildSelectable(int groupPosition, int childPosition) {  
        return true;  
    }  
  
    public boolean hasStableIds() {  
        return true;  
    }  

}

